package com.ebring.shareEducation.service.impl;


import com.ebring.shareEducation.bean.WalletAdds;
import com.ebring.shareEducation.dao.WalletAddsMapper;
import com.ebring.shareEducation.service.WalletAddsService;
import com.ebring.shareEducation.utils.RspMsg;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class WalletAddsServiceImpl implements WalletAddsService {

    @Autowired
    private WalletAddsMapper walletAddsMapper;
    //查询所有的角色
    @Override
    public RspMsg<List<WalletAdds>> selectWallet() {
        Map<String, Object> map = new LinkedHashMap<>();
        RspMsg rspMsg = new RspMsg();
        List<WalletAdds> WalletAddslist = walletAddsMapper.selectAll();
        if (WalletAddslist.isEmpty() || WalletAddslist == null) {
            rspMsg.setOK();
            rspMsg.setMsg("未找到数据");
            return rspMsg;

        }



        PageInfo<WalletAdds> pageInfo = new PageInfo<WalletAdds>(WalletAddslist);
        // 总条数
        map.put("total", pageInfo.getTotal());
        // 每页显示条数
        map.put("curPage", pageInfo.getPageSize());
        // 总页数
        map.put("pages", pageInfo.getPages());
        // 当前页
        map.put("pageNum", pageInfo.getPageNum());
        map.put("WalletAddslist", WalletAddslist);
        rspMsg.setOK();
        rspMsg.setResult(map);
        return rspMsg;

    }


    //根据用户id查询钱包地址
    @Override
    public RspMsg<List<WalletAdds>> selectWalletByUserId(String userid) {
        Map<String, Object> map = new LinkedHashMap<>();
        RspMsg rspMsg = new RspMsg();
        List<WalletAdds> walletList = walletAddsMapper.selectCRByUserWallet(userid);
        if (walletList.isEmpty() || walletList == null) {
            rspMsg.setOK();
            rspMsg.setMsg("未找到数据");
            return rspMsg;
        }
        map.put("walletList", walletList);
        rspMsg.setOK();
        rspMsg.setResult(map);
        return rspMsg;

    }


    //修改角色
    @Override
    public RspMsg<WalletAdds> updateWallet(WalletAdds walletAdds) {
        RspMsg rspMsg = new RspMsg();
        int i = walletAddsMapper.updateByPrimaryKeySelective(walletAdds);
        if (i == 0) {
            rspMsg.setError();
            rspMsg.setMsg("修改失败");
            return rspMsg;
        }
        rspMsg.setOK();
        rspMsg.setMsg("修改成功");

        return rspMsg;

    }
    //新增加角色
    @Override
    public RspMsg<WalletAdds> insetWallet(WalletAdds walletAdds) {
        RspMsg rspMsg = new RspMsg();
        int i = walletAddsMapper.insertSelective(walletAdds);
        if (i == 0) {
            rspMsg.setError();
            rspMsg.setMsg("新增失败");
            return rspMsg;
        }
        rspMsg.setOK();
        rspMsg.setMsg("新增成功");

        return rspMsg;

    }
    //模糊查询角色管理
    @Override
    public RspMsg<List<WalletAdds>> select(String userid, String bid) {
        Example example = new Example(WalletAdds.class);
        if ((userid != null && !"".equals(userid))&&(bid != null && !"".equals(bid))){
            example.createCriteria().andLike("userid", "%"+userid+"%").andLike("bid", "%"+bid+"%");
        }
        if (userid != null && !"".equals(userid)){
            example.createCriteria().andLike("userid", "%"+userid+"%");
        }
        if (bid != null && !"".equals(bid)){
            example.createCriteria().andLike("bid", "%"+bid+"%");
        }

        RspMsg rspMsg = new RspMsg();
        Map<String, Object> map = new LinkedHashMap<>();
        List roleList = walletAddsMapper.selectByExample(example);
        if (roleList.isEmpty() || roleList == null) {
            rspMsg.setOK();
            rspMsg.setMsg("未找到相关用户");
            return rspMsg;
        }

        PageInfo pageInfo = new PageInfo<>(roleList);

        // 总条数
        map.put("total", pageInfo.getTotal());
        // 每页显示条数
        map.put("curPage", pageInfo.getPageSize());
        // 总页数
        map.put("pages", pageInfo.getPages());
        // 当前页
        map.put("pageNum", pageInfo.getPageNum());
        map.put("roleList", roleList);
        rspMsg.setOK();
        rspMsg.setResult(map);
        return rspMsg;

    }

    // 批量删除用户
    @Override
    public RspMsg<WalletAdds> DeleteWallet(String id) {

        RspMsg rspMsg = new RspMsg();
        int sum = 0;
        sum=walletAddsMapper.deleteByPrimaryKey(id);
        if (sum > 0) {
            rspMsg.setOK();
            rspMsg.setMsg("删除成功");

            rspMsg.setResult(sum);
            return rspMsg;
        }

        rspMsg.setError();
        rspMsg.setMsg("删除失败");
        return rspMsg;
    }





}

